﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QLNS.aspFrm
{
    public partial class frmDanhSachCBCCVCDuocTangLuong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            System.Data.DataTable table = MakeTable();
            var query = context.GetDanhSachCBCCVCTangLuong(DateTime.Parse(Request.QueryString["nam"]));
            int sobacduocnang;
            foreach (var i in query)
            {
                if (i.SoNgay >= 0)
                {
                    System.Data.DataRow row;
                    row = table.NewRow();
                    row["IdDonVi"] = i.IDDonVi;
                    row["TenDonVi"] = i.TenDonVi;
                    row["HoTen"] = i.HoTen;
                    row["TenChucVu"] = i.TenChucVuc;
                    if (i.GioiTinh == "Nam")
                    {
                        row["GioiTinhNam"] = i.NgaySinh;
                    }
                    else if (i.GioiTinh == "Nữ")
                    {
                        row["GioiTinhNu"] = i.NgaySinh;
                    }
                    row["ChuyenMon"] = i.ChuyenMon;
                    row["NgayHuongLuong"] = String.Format("{0:dd/MM/yyyy}", i.NgayDuocHuongLuong);
                    row["BacHienHuong"] = Convert.ToString(i.SoBacHienHuong) + '/' + Convert.ToString(i.SoBac);
                    row["MaNgachCC"] = i.MaNgachCC;
                    row["HeSoHienHuong"] = i.HeSoHienHuong;
                    sobacduocnang = Convert.ToInt32(i.SoBacHienHuong) + Convert.ToInt32(i.SoBacDuocHuong);
                    if (sobacduocnang > i.SoBac)
                    {
                        row["BacNangLuong"] = Convert.ToString(i.SoBac) + '/' + Convert.ToString(i.SoBac);
                        row["HeSoNangLuong"] = i.HeSoGoc + ((i.SoBac - 1) * i.HeSoTang);
                    }
                    else
                    {
                        row["BacNangLuong"] = Convert.ToString(sobacduocnang) + '/' + Convert.ToString(i.SoBac);
                        row["HeSoNangLuong"] = i.HeSoGoc + ((sobacduocnang - 1) * i.HeSoTang);
                        string ngayduochuongluong = String.Format("{0:dd/MM/yyyy}", i.NgayDuocHuongLuong);
                        int day = DateTime.Parse(ngayduochuongluong).Day;
                        int month = DateTime.Parse(ngayduochuongluong).Month;
                        int year = DateTime.Parse(ngayduochuongluong).Year;
                        for (int j = 0; j < i.SoBacDuocHuong; j++)
                        {
                            year +=Convert.ToInt32(i.SoNamTangLuong);
                        }
                        row["NgayDuocNangLuong"] = Convert.ToString(day)+'/'+Convert.ToString(month)+'/'+Convert.ToString(year);
                    }
                    table.Rows.Add(row);
                }
            }
           
            CrystalDecisions.CrystalReports.Engine.ReportDocument rd = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            string strRptPath = Server.MapPath("~/") + "Reports//Report_DanhSachCanBoTangLuong.rpt";
            //Loading Report
            rd.Load(strRptPath);

            // Setting report data source
            if (table != null)
                rd.SetDataSource(table);
            CrystalReportViewer1.ReportSource = rd;
        }

        private System.Data.DataTable MakeTable()
        {
            System.Data.DataTable table = new System.Data.DataTable("Table");
            System.Data.DataColumn column;

            // Create new DataColumn, set DataType, 

            // Create second column.

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "IdCanBo";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "HoTen";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "NgaySinh";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "GioiTinhNam";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "GioiTinhNu";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "TenChucVu";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "ChuyenMon";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "NgayHuongLuong";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "BacHienHuong";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "MaNgachCC";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.Double");
            column.ColumnName = "HeSoHienHuong";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "BacNangLuong";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "HeSoNangLuong";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "NgayDuocNangLuong";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "TenDonVi";
            // Add the column to the table.
            table.Columns.Add(column);

            column = new System.Data.DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "IdDonVi";
            // Add the column to the table.
            table.Columns.Add(column);

            return table;
        }
    }
}